/**
 * @(#)MapViewer.java        1.0 4/11/12
 * 
 * Copyright (c) 2012 public static droid main
 * Georgia Institute of Technology, Atlanta, GA
 * All rights reserved.
 */
package edu.gatech.oad.publicstaticdroidmain.taskmanager.ui;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import edu.gatech.oad.publicstaticdroidmain.taskmanager.model.LocationHelper;
import edu.gatech.oad.publicstaticdroidmain.taskmanager.model.Task;
import edu.gatech.oad.publicstaticdroidmain.taskmanager.model.TaskManager;

/**
 * Displays a map with the locations of all of a user's tasks
 * plotted.
 * 
 * @author Matt Giannelli
 */
public class MapViewer extends MapActivity {

    private MapView mapView;
    private List<Overlay> mapOverlays;
    private Drawable drawable;
    private MapOverlay itemizedOverlay;
    private TaskManager taskManager;

    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.map_view);

	mapView = (MapView) findViewById(R.id.mapview);
	mapView.setBuiltInZoomControls(true);

	taskManager = ((TaskManagerApplication)getApplication()).getTaskManager();

	mapOverlays = mapView.getOverlays();
	drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
	itemizedOverlay = new MapOverlay(drawable);

	displayTaskLocations();
    }

    /**
     * Plots all of the user's Tasks on the map.
     */
    private void displayTaskLocations() {
	Task[] allTasks = taskManager.getAllTasks();

	for (Task task : allTasks) {
	    double latitude = task.getLatitude();
	    double longitude = task.getLongitude();

	    Toast.makeText(this, String.format("lat: %f, long: %f", latitude, longitude), Toast.LENGTH_LONG);

	    if (LocationHelper.isValidLatitude(latitude) && LocationHelper.isValidLongitude(longitude)) {
		GeoPoint point = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
		OverlayItem overlayItem = new OverlayItem(point, task.getName(), task.getDescription());

		itemizedOverlay.addOverlay(overlayItem);
		mapOverlays.add(itemizedOverlay);
	    }
	}
    }


    /* (non-Javadoc)
     * @see com.google.android.maps.MapActivity#isRouteDisplayed()
     */
    @Override
    protected boolean isRouteDisplayed() {
	// TODO Auto-generated method stub
	return false;
    }

}
